package cn.mumzone.pos.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TransactionRecordEntity;
import cn.mumzone.pos.presenter.TransactionRecordPresenter;
import cn.mumzone.pos.presenter.contract.TransactionRecordContract;
import cn.mumzone.pos.ui.adapter.ADA_TransactionRecord;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.constant.RefreshState;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;

/**
 * Created by qiuzhenhuan on 2019/8/1.
 */

public class FRA_TransactionRecord extends BaseFragment<TransactionRecordPresenter> implements TransactionRecordContract.View {

    private static final String KEY_TYPE = "transaction_type";
    private static final String KEY_MEMBER_ID = "member_id";

    //0-充值，2-提现，3-转账 4-余额消费 5、退款 8、全部
    public static final int TYPE_RECHARGE = 0;
    public static final int TYPE_CONSUMPTION = 4;
    public static final int TYPE_REFUND = 5;
    public static final int TYPE_ALL = 8;


    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_record)
    LRecyclerView rvRecord;
    @BindView(R.id.ll_record)
    LinearLayout llRecord;

    private int transactionType;

    private int mPage = 1;

    private String memberId;

    private long dateTime;

    private ADA_TransactionRecord mAdapter;
    private LRecyclerViewAdapter mRvAdapter;


    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        transactionType = extras.getInt(KEY_TYPE, TYPE_ALL);
        memberId = extras.getString(KEY_MEMBER_ID);
    }

    @Override
    protected void onUserVisible() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        if (transactionType == TYPE_ALL) {
            refreshLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    refreshLayout.autoRefresh();
                }
            }, 300);
        } else {
            refreshLayout.autoRefresh();
        }
    }

    @Override
    protected void initEventAndData() {
        dateTime = System.currentTimeMillis();
        rvRecord.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new ADA_TransactionRecord(mContext);
        mRvAdapter = new LRecyclerViewAdapter(mAdapter);
        rvRecord.setAdapter(mRvAdapter);
        rvRecord.setPullRefreshEnabled(false);
        rvRecord.setLoadMoreEnabled(false);
        rvRecord.setHasFixedSize(true);
        rvRecord.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                mPresenter.loadRecordList(false, String.valueOf(transactionType), memberId,
                        AppDateUtil.getTimeStamp(dateTime, AppDateUtil.YYYY_MM_DD), String.valueOf(mPage));
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                rvRecord.setNoMore(false);
                mPresenter.loadRecordList(true, String.valueOf(transactionType), memberId,
                        AppDateUtil.getTimeStamp(dateTime, AppDateUtil.YYYY_MM_DD), String.valueOf(mPage));
            }
        });

        toggleShowEmpty(true, R.mipmap.ic_empty_record, "暂无交易记录哦～", "");
    }

    public void refresh() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_transaction_record;
    }


    public static FRA_TransactionRecord getInstance(int transactionType, String memberId) {
        FRA_TransactionRecord fragment = new FRA_TransactionRecord();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_TYPE, transactionType);
        bundle.putString(KEY_MEMBER_ID, memberId);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void setDateTime(long dateTime) {
        this.dateTime = dateTime;
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        refreshLayout.finishRefresh();
    }

    @Override
    protected View getTargetView() {
        return llRecord;
    }


    @Override
    public void showRecordList(List<TransactionRecordEntity> list, boolean isRefresh) {
        refreshLayout.finishRefresh();
        mAdapter.update(list, isRefresh);
        mRvAdapter.notifyDataSetChanged();
        if (!ListUtil.isListEmpty(list)) {
            mPage++;
        } else {
            rvRecord.setNoMore(true);
        }
        if (ListUtil.isListEmpty(mAdapter.getDataList())) {
            toggleShowEmpty(true, R.mipmap.ic_empty_record, "暂无交易记录哦～", "");
        } else {
            toggleShowEmpty(false);
        }
    }


    public boolean isRefreshing() {
        return refreshLayout.getState() != RefreshState.None;
    }
}
